package androidApplication.javaFiles;

import C2DMRelated.RegistrationHandler;
import ChatPackage.ChatHandler;
import MapRelated.MyMapView;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

public class MainActivity extends MapActivity
{
	//***************************************Class Variables*******************************************//


	private static String _tag = new String("MainActivity");
	//private MapManager _mapManager;
	private MyMapView _mapView;
	private static int BACKGROUND = 0;
	private static int EXIT = 1;
	private static int CANCEL = 2;


	//***************************************Constructors*******************************************//


	//***************************************Public Methods*******************************************//


	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		Log.v(_tag,"onCreate.........");

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		_mapView = (MyMapView) findViewById(R.id.mapView);

		this.registerForContextMenu(_mapView); //for the back button in the map.

		//initially register to the c2dm service
		(new RegistrationHandler()).registerToC2DM(this);

		Log.v(_tag,"Finished Creating.........");
	}

	// TODO Delete
	private void myTesting()
	{
		
	}


	@Override
	public void onNewIntent(Intent intent)
	{
		Log.v(_tag, "A new intent was received!");

		if(intent.getStringExtra("finished registration!") != null)
		{
			Log.v(_tag, "finished registration intent received!");

			_mapView.initAfterConnection();

			// this function can be used to make tests
			//			myTesting();
		}
		else
		{
			super.onNewIntent(intent);
		}
	}


	//we need to do this if we don't want to load the activity from beginning every change of configuration 
	@Override
	public void onConfigurationChanged(Configuration newConfig)
	{
		super.onConfigurationChanged(newConfig);
	}




	//***************************************Private Methods*******************************************//	
	/*
	 * this function gets one google account from this device, in the future we can had a mechanism that allows
	 * the user to choose which account he is using, and the current function may stay a DEFAULT
	 */

	//***************************************Protected Methods*******************************************//	
	@Override
	protected boolean isRouteDisplayed() 
	{
		// TODO Auto-generated method stub
		return true;
	}


	/**
	 * for the Back Menu
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)  
	{
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			// do something on back.
			Log.v("MainActivity","you pressed the back button");
			this.openContextMenu((MapView) findViewById(R.id.mapView));
			//return super.onKeyDown(keyCode, event);//true;
		}

		return super.onKeyDown(keyCode, event);
	}


	/**
	 * for the Back Menu
	 */
	@Override  
	public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) 
	{  
		super.onCreateContextMenu(menu, v, menuInfo);  
		menu.setHeaderTitle("What do you want to do?");  
		menu.add(Menu.NONE, BACKGROUND, Menu.NONE, "Run Lookie2 in background");   
		menu.add(Menu.NONE, EXIT, Menu.NONE, "Exit Lookie2");
		menu.add(Menu.NONE, CANCEL, Menu.NONE, "Cancel");
	} 


	/**
	 * for the Back Menu
	 */
	@Override  
	public boolean onContextItemSelected(MenuItem item) {  
		if(item.getItemId()==BACKGROUND){
			Log.v("MainActivity","you pressed BACKGROUND");
			//how to we do that??
			moveTaskToBack(true);
		}
		if(item.getItemId()==EXIT){
			Log.v("MainActivity","you pressed EXIT");
			finish(); //to finish activity
			System.exit(0);
		}
		if(item.getItemId()==CANCEL){
			Log.v("MainActivity","you pressed CANCEL");
			//nothing to be done...
		}
		else {return false;}

		return super.onContextItemSelected(item);  
	}


	//don't delete - ask Tal

	/**
	 * this method defines the menu that appear when you
	 * click the menu button on the mainActivity.
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		Log.v("MainActivity","you pressed menu");
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_button, menu);

		return super.onCreateOptionsMenu(menu);
	}

	/**
	 * for the Menu button
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) 
	{
		// Handle item selection
		switch (item.getItemId()) 
		{
		case R.id.chats:
			Log.v("MainActivity","you pressed chats");
			ChatHandler.startUsersListActivity(_mapView.getContext());
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}
	}

}